package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.google.common.collect.Maps;
import com.rules.admin.entity.Rule;
import com.rules.admin.entity.RuleAssembly;
import com.rules.admin.dao.RuleAssemblyDao;
import com.rules.admin.service.RuleService;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.service.RuleAssemblyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class RuleAssemblyServiceImpl extends ServiceImpl<RuleAssemblyDao, RuleAssembly> implements RuleAssemblyService {

   @Autowired
   private RuleService ruleService;
   @Autowired
   private RulePackageService rulePackageService;



   public RuleAssembly insert(RuleAssembly ruleAssembly) {
      this.save(ruleAssembly);
      return ruleAssembly;
   }

   public RuleAssembly update(RuleAssembly ruleAssembly) {
      this.updateById(ruleAssembly);
      return ruleAssembly;
   }

   public void delete(long id) {
      this.removeById(id);
      this.ruleService.deleteByRuleAssembly(id);
   }

   public void deleteByRulePackage(long rulePackageId) {

      LambdaQueryWrapper<RuleAssembly> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RuleAssembly::getPackageId, rulePackageId);
      List<RuleAssembly> ruleAssemblies = this.list(queryWrapper);
      Iterator iterator = ruleAssemblies.iterator();
      while(iterator.hasNext()) {
         RuleAssembly ruleAssembly = (RuleAssembly)iterator.next();
         this.delete(ruleAssembly.getId());
      }

   }



   public IPage<RuleAssembly> queryPage(long rulePackageId, String name, IPage<RuleAssembly> page) {
      LambdaQueryWrapper<RuleAssembly> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RuleAssembly::getPackageId, rulePackageId);
      if (StringUtils.isNotBlank(name)) {
         queryWrapper.like(RuleAssembly::getName,"%" + name + "%");
      }
      queryWrapper.orderByAsc(RuleAssembly::getPriority);
      return this.page(page, queryWrapper);
   }

   public List<RuleAssembly> getRuleAssemblyByPackageId(long rulePackageId) {
      LambdaQueryWrapper<RuleAssembly> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RuleAssembly::getPackageId, rulePackageId);
      queryWrapper.orderByDesc(RuleAssembly::getPriority);
      queryWrapper.orderByAsc(RuleAssembly::getCreateTime);
      return this.list(queryWrapper);
   }

   public List<RuleAssembly> getRuleAssemblyByPackageIdAndStatus(long rulePackageId) {
      LambdaQueryWrapper<RuleAssembly> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RuleAssembly::getPackageId, rulePackageId);
      queryWrapper.eq(RuleAssembly::getAssemblyStatus,0);
      queryWrapper.orderByDesc(RuleAssembly::getPriority);
      queryWrapper.orderByAsc(RuleAssembly::getCreateTime);
      return this.list(queryWrapper);
   }

   public List<Map<String, Object>> getRuleAssemblyMenu(long rulePackageId) {
      List<RuleAssembly> assemblies = this.getRuleAssemblyByPackageId(rulePackageId);
      List<Map<String, Object>> result = new ArrayList<>(assemblies.size());
      assemblies.stream().forEach(item->{
         Map<String, Object> assembly = Maps.newHashMap();
         List<Rule> rules = this.ruleService.getRuleListByAssemblyId(item.getId());
         assembly.put("id", item.getId() + "");
         assembly.put("name", item.getName());
         assembly.put("priority", item.getPriority());
         assembly.put("rules", rules);
         assembly.put("type", 0);
         result.add(assembly);
      });
      return result;
   }

   public void create(RuleAssembly ruleAssembly) {
      ruleAssembly.setPriority(this.rulePackageService.getPriority(ruleAssembly.getPackageId()));
      this.save(ruleAssembly);
   }
}
